/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant.ant.types;


import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.selectors.FileSelector;
import org.apache.tools.ant.util.FileUtils;


/**
 * <p>
 * This class is a composite fileset for Ant 1.6.1 that allows
 * filesets to be children of it.
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class CompositeFileset extends FileSet {

    List filesets = new ArrayList();


    public CompositeFileset() {
    }

    protected CompositeFileset(FileSet fileSet) {
        super(fileSet);
    }


    public void addFileset(FileSet fs) throws BuildException {
        filesets.add(fs);
    }

    public DirectoryScanner getDirectoryScanner(Project p) {
        return new CompositeDirectoryScanner(filesets, p);
    }


    public static class CompositeDirectoryScanner extends DirectoryScanner {

        private List fileList = new ArrayList();


        public CompositeDirectoryScanner(List filesets, Project p) {
            Iterator iter = filesets.iterator();
            FileSet fs;
            DirectoryScanner ds;
            String[] files;
            File dir;
            while (iter.hasNext()) {
                fs = (FileSet) iter.next();
                ds = fs.getDirectoryScanner(p);

                files = ds.getIncludedFiles();
                dir = ds.getBasedir();
                for (int i = 0; i < files.length; i++) {
                    fileList.add(dir.getAbsolutePath() + File.separator + files[i]);
                    p.log("Added file: " + dir.getAbsolutePath() + File.separator +
                        files[i], Project.MSG_DEBUG);
                }
            }
        }


        /** Everything is absolute */
        public File getBasedir() {
            File here = new File(".");
            File parent = null;
            while (here.getParentFile() != null) {
                parent = here.getParentFile();
            }

            return parent;
        }

        public void setBasedir(String basedir) {
            // No-op
        }

        public void setBasedir(File basedir) {
            // No-op
        }

        public boolean isCaseSensitive() {
            return true;
        }

        public void setCaseSensitive(boolean isCaseSensitive) {
            // No-op
        }

        public boolean isFollowSymlinks() {
            return true;
        }

        public void setFollowSymlinks(boolean followSymlinks) {
            // No-op
        }

        public void setIncludes(String[] includes) {
            // No-op
        }

        public void setExcludes(String[] excludes) {
            // No-op
        }

        public void setSelectors(FileSelector[] selectors) {
            // No-op
        }

        public boolean isEverythingIncluded() {
            return true;
        }

        protected boolean isIncluded(String name) {
            return fileList.contains(name);
        }

        protected boolean isExcluded(String name) {
            return !fileList.contains(name);
        }

        protected boolean isSelected(String name, File file) {
            return false;
        }

        public String[] getIncludedFiles() {
            return (String[]) fileList.toArray(new String[fileList.size()]);
        }

        public String[] getNotIncludedFiles() {
            return new String[0];
        }

        public String[] getExcludedFiles() {
            return new String[0];
        }

        public String[] getDeselectedFiles() {
            return new String[0];
        }

        public String[] getIncludedDirectories() {
            return new String[0];
        }

        public String[] getNotIncludedDirectories() {
            return new String[0];
        }

        public String[] getExcludedDirectories() {
            return new String[0];
        }

        public String[] getDeselectedDirectories() {
            return new String[0];
        }

        public Resource getResource(String name) {
            File f = FileUtils.newFileUtils().resolveFile(basedir, name);
            return new Resource(name, f.exists(), f.lastModified(),
                                f.isDirectory());
        }

        public void addDefaultExcludes() {
            // No-op
        }

        protected boolean couldHoldIncluded(String name) {
            return false;
        }

        public void scan() throws IllegalStateException {
            // No-op
        }

        protected void slowScan() {
            // No-op
        }

        protected void scandir(File dir, String vpath, boolean fast) {
            // No-op
        }
    }
}